'use strict'
$('#register').click(function() {
    let username = $('#user').val();
    let pwd = $('#pwd').val();
    let confirmPwd = $('#confirmPwd').val();

    if (username.length > 0 && pwd.length > 0 && pwd === confirmPwd) {
        $.ajax({
            url: '/registerDone',
            type: 'post',
            data: {
                username: username,
                pwd: pwd,
                confirmPwd: confirmPwd
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 200) {
                    window.location.href = '/login'
                } else {
                    alter_fn(res.msg)
                }
            },
            error: function(res) {
                alter_fn(res.msg)
            }
        })
    } else {
        alter_fn('The password cannot be empty, and the password should be the same twice.');
    }
});


$('#cancel').click(function() {
    window.location.href = '/login'
});

function alter_fn(msg) {
    $('#msg').text(msg);
    $('.invisible').removeClass('invisible');
    setTimeout(() => {
        $('.alert').addClass('invisible');
    }, 3000);
}